home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Misc / Wood.0.72 / Sources / LispDoc.bproj / parser.ym < prev    next >
Encoding:
Text File  |  1994-06-05  |  2.2 KB  |  135 lines

  1.  
  2. %{
  3.  
  4. #import <ansi/stdlib.h>
  5. #import <ansi/string.h>
  6. #import <ansi/stdio.h>
  7. #import <ansi/ctype.h>
  8. #import <ansi/math.h>
  9.  
  10. #import "../Tree.h"
  11.  
  12. extern void lispdocPrepareTextToScan(NXStream *text);
  13. extern yylex(), yyparse();
  14.  
  15. static void yyerror(char *s);
  16. static NXZone *treeZone;
  17. static Properties *treeProps;
  18. static Tree *resultTree;
  19. static Tree *lispdocMakeTree(char *name, char *description);
  20. static Tree *lispdocAddChildren(Tree *tree, List *children);
  21. static List *lispdocGatherSibling(List *list, Tree *sibling);
  22. static List *lispdocMakeChildrenList(Tree *child);
  23.  
  24. %}
  25.  
  26. %union{
  27.     Tree *tree;
  28.     char *name;
  29.     List *list;
  30. }
  31.  
  32. %token <name> NAME DESCRIPTION 
  33. %token LISPKEY
  34. %left LP RP
  35.  
  36. %type <tree> lispnode lispdoc lispbody
  37. %type <list> nodelist
  38.  
  39. %%
  40.  
  41. lispdoc:    lispnode
  42.             {
  43.                 resultTree = $1;
  44.             }
  45.             | /* empty */
  46.             {
  47.                 resultTree = nil;
  48.             }
  49.             ;
  50.  
  51. lispnode:     LP lispbody nodelist RP
  52.             {     
  53.                 $$ = lispdocAddChildren($2, $3); 
  54.             }    
  55.             | LP lispbody RP
  56.             {     
  57.                 $$ = $2; 
  58.             }
  59.             |     LP RP
  60.             {    
  61.                 $$ = nil;
  62.             }    
  63.             ;
  64.  
  65. lispbody:    LISPKEY NAME LP DESCRIPTION RP
  66.             {
  67.                 $$ = lispdocMakeTree($2, $4);
  68.                 free($2);
  69.                 free($4);
  70.             }
  71.             ;
  72.  
  73. nodelist:    nodelist lispnode
  74.             {
  75.                 $$ = lispdocGatherSibling($1, $2);
  76.             }
  77.             | lispnode
  78.             {
  79.                 $$ = lispdocMakeChildrenList($1);
  80.             }
  81.             ;
  82.     
  83. %%
  84.  
  85. static Tree *lispdocMakeTree(char *name, char *description)
  86. {             
  87.     return [[[Tree allocFromZone:treeZone] 
  88.              initLabel:name props:treeProps] setDescription:description]; 
  89. }
  90.  
  91. static Tree *lispdocAddChildren(Tree *tree, List *children)
  92. {
  93.     Tree *child;
  94.     int i;
  95.     
  96.     for(i = 0; i < [children count]; i++){
  97.         child = (Tree *)[children objectAt:i];
  98.         [tree addTree:child];
  99.     }
  100.     [children free];    
  101.     return tree;
  102. }
  103.  
  104. static List *lispdocGatherSibling(List *list, Tree *sibling)
  105. {
  106.     [list addObject:sibling];
  107.     return list;
  108. }
  109.  
  110. static List *lispdocMakeChildrenList(Tree *child)
  111. {
  112.     List *list;
  113.  
  114.     list = [[List alloc] init];
  115.     [list addObject:child];
  116.     return list;
  117. }
  118.  
  119. id createLispTree(NXStream *stream, Properties *props, NXZone *zone)
  120. {    
  121.     int r;
  122.  
  123.     lispdocPrepareTextToScan(stream);
  124.     treeZone = zone;
  125.     treeProps = props;
  126.     r = yyparse();
  127.     return resultTree;
  128. }
  129.     
  130. static void yyerror(char *s)
  131. {
  132.     printf("parser error:%s\n",s);
  133. }
  134.  
  135.